iT邦幫忙

1

[筆記系列]MySql + Entiy Framework 4.5.2 Code-First(Migrations)

  • 分享至 

  • xImage
  •  

參考資料:
https://zhuanlan.zhihu.com/p/43190042
https://www.c-sharpcorner.com/blogs/mysqlentityframeworkcodefirst-for-net-developers
https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html

首先:版本需要mapping
.Net Framework 4.5.2
EntityFramework 6.2.0
MySql.Data 8.0.14
MySql.EntityFramework 8.0.14
(只需要以上)
修改 Web.Config

  <connectionStrings>
    <add name="mysqlConnect" connectionString="Datasource=.;port=20201;uid=admin;pwd=admin;Database=db;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  
    <entityFramework codeConfigurationType="MySql.Data.EntityFramework.MySqlEFConfiguration, MySql.Data.EntityFramework">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework" />
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
  </entityFramework>
  
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL"
           type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

--就這樣

PM>Enable-Migrations 就可以成功了

在Configuration.cs中加入
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.EntityFramework.MySqlMigrationSqlGenerator());

{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<MamawayWeb.Models.DbEntity.MamawayContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;

        SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.EntityFramework.MySqlMigrationSqlGenerator());
    }

    protected override void Seed(MamawayWeb.Models.DbEntity.MamawayContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data.
    }
}

}

之後第一次
Add-Migration InitialCreate –IgnoreChanges

Add-migration就能成功囉
接著update-database


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言